package com.erp.erp_mask.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.erp.erp_mask.mapper.TPurapplyMapper;
import com.erp.erp_mask.mapper.TPurcheckMapper;
import com.erp.erp_mask.pojo.TPurapply;
import com.erp.erp_mask.service.TPurapplyService;
import com.erp.erp_mask.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @BelongsProject: erp
 * @BelongsPackage: com.erp.erp_mask.controller
 * @Author: wang
 * @CreateTime: 2024-05-15  09:52
 * @Description: TODO
 * @Version: 1.0
 * @effect:
 */
@RestController
@RequestMapping("/pur")
public class TPurapplyController {
    @Resource
    private TPurapplyMapper tPurapplyMapper;
    @Resource
    private TPurcheckMapper tPurcheckMapper;
    @GetMapping("/purlist")
    public ModelAndView index() {
        return new ModelAndView("/pur/purApply");
    }
    @GetMapping("/searchList")
    public Result searchList(String startdate,String overdate){
        LambdaQueryWrapper<TPurapply> wrapper = Wrappers.lambdaQuery();
        if (StringUtils.hasLength(startdate)&&StringUtils.hasLength(overdate)){
            wrapper.between(TPurapply::getPurDate,startdate,overdate);
            return Result.success(tPurapplyMapper.search(wrapper));
        }else{
            return Result.success(tPurapplyMapper.search(wrapper));
        }
    }
    @PostMapping("/addList")
    @Transactional
    public Result addList(@RequestBody TPurapply tPurapply){
        System.out.println(tPurapply);
        Integer price= tPurapply.getPurPrice();
        Integer number = tPurapply.getPurNumber();
        Integer count=price*number;
        System.out.println(count);
        tPurapply.setPurCount(count);
        try{
            tPurapplyMapper.addlist(tPurapply);
            tPurcheckMapper.addlist(tPurapply);
            return Result.success("申请提交成功!");
        }catch (Exception e){
            return Result.fail("申请提交失败!");
        }
    }
    @GetMapping("/selectnum")
    public Result selectnum(String name){
        return Result.success(tPurapplyMapper.selectnum(name));
    }
}